//
//  FindPWDVC.swift
//  旅航ios
//
//  Created by tulip on 2021/5/17.
//

import UIKit

class FindPWDVC: CommonVC {

    var isResetPWD=false
    var type:String!
    var PhoneText:UITextField!
    var MessageText:UITextField!
    var MessageBtn:UIButton!
    var pwdText:UITextField!
    var RePwdText:UITextField!
    var codeType:String!
    var timer :Timer!
    var leftTime:Int = 60
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if  isResetPWD{
          //  titleLab.text="修改密码"

        }else{
          //  titleLab.text="找回密码"

        }
        head.title.text="修改密码"
        
        // Do any additional setup after loading the view.
        let scrollView=UIScrollView.init(frame: CGRect(x: 0, y: TopSpaceHeight, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-TopSpaceHeight))
        
        self.view.addSubview(scrollView)
        
        let line0=UIView.init(frame: CGRect(x: 0, y:  0, width: SCREEN_WIDTH, height: 1))
        line0.backgroundColor=UIColor.init(hexString: "ebebeb")
        scrollView.addSubview(line0)
        
        
        //手机号
        let phoneLab=UILabel.init(frame: CGRect(x: 25, y:  15, width: 55, height: 20))
        phoneLab.text="手机号"
        phoneLab.textColor=UIColor.init(hexString: "666666")
        phoneLab.font=UIFont.systemFont(ofSize: 15)
        scrollView.addSubview(phoneLab)
        
        PhoneText=UITextField.init(frame: CGRect(x: 105, y:  15, width: SCREEN_WIDTH-105, height: 20))
        PhoneText.placeholder="请输入手机号码"
        PhoneText.textColor=UIColor.init(hexString: "666666")
        PhoneText.font=UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(PhoneText)
        
        let line1=UIView.init(frame: CGRect(x: 15, y:  phoneLab.frame.size.height+phoneLab.frame.origin.y+15, width: SCREEN_WIDTH-30, height: 1))
        line1.backgroundColor=UIColor.init(hexString: "ebebeb")
        scrollView.addSubview(line1)
        
        //验证码
        let MessageLab=UILabel.init(frame: CGRect(x: 25, y: line1.frame.origin.y+15, width: 55, height: 20))
        MessageLab.text="验证码"
        MessageLab.textColor=UIColor.init(hexString: "666666")
        MessageLab.font=UIFont.systemFont(ofSize: 15)
        scrollView.addSubview(MessageLab)
        
        MessageText=UITextField.init(frame: CGRect(x: 105, y: line1.frame.origin.y+15, width: SCREEN_WIDTH-105-95, height: 20))
        MessageText.placeholder="请输入短信验证码"
        MessageText.textColor=UIColor.init(hexString: "666666")
        MessageText.font=UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(MessageText)
        
        MessageBtn=UIButton.init(frame: CGRect(x: SCREEN_WIDTH-95, y:line1.frame.origin.y+15 , width: 70, height: 20))
        MessageBtn.setTitle("获取验证码", for: .normal)
        MessageBtn.titleLabel?.font=UIFont.systemFont(ofSize: 13)
        MessageBtn.setTitleColor(UIColor.init(hexString: "333333"), for: .normal)
        MessageBtn.addTarget(self, action: #selector(getVerify), for: .touchUpInside)
        scrollView.addSubview(MessageBtn)
    
        
        let line2=UIView.init(frame: CGRect(x: 15, y: MessageText.frame.origin.y+35, width: SCREEN_WIDTH-34, height: 1))
        line2.backgroundColor=UIColor.init(hexString: "ebebeb")
        scrollView.addSubview(line2)
        
        //登录密码
        let pwdLab=UILabel.init(frame: CGRect(x: 25, y: line2.frame.origin.y+15, width: 55, height: 20))
        pwdLab.text="新密码"
        pwdLab.textColor=UIColor.init(hexString: "666666")
        pwdLab.font=UIFont.systemFont(ofSize: 15)
        scrollView.addSubview(pwdLab)
        
        pwdText=UITextField.init(frame: CGRect(x: 105, y: line2.frame.origin.y+15, width: SCREEN_WIDTH-105, height: 20))
        pwdText.placeholder="请输入新密码"
        pwdText.textColor=UIColor.init(hexString: "666666")
        pwdText.font=UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(pwdText)
        
        
        
        let line3=UIView.init(frame: CGRect(x: 15, y: pwdText.frame.origin.y+35, width: SCREEN_WIDTH-34, height: 1))
        line3.backgroundColor=UIColor.init(hexString: "ebebeb")
        scrollView.addSubview(line3)
        
        
        //确认密码
        let RePwdLab=UILabel.init(frame: CGRect(x: 25, y: line3.frame.origin.y+15, width: 70, height: 15))
        RePwdLab.text="确认密码"
        RePwdLab.textColor=UIColor.init(hexString: "666666")
        RePwdLab.font=UIFont.systemFont(ofSize: 15)
        scrollView.addSubview(RePwdLab)
        
        RePwdText=UITextField.init(frame: CGRect(x: 105, y: line3.frame.origin.y+15, width: SCREEN_WIDTH-105, height: 20))
        RePwdText.placeholder="请再次输入密码"
        RePwdText.textColor=UIColor.init(hexString: "666666")
        RePwdText.font=UIFont.systemFont(ofSize: 16)
        scrollView.addSubview(RePwdText)
        
        
        let line4=UIView.init(frame: CGRect(x: 15, y: RePwdText.frame.origin.y+35, width: SCREEN_WIDTH-34, height: 1))
        line4.backgroundColor=UIColor.init(hexString: "ebebeb")
        scrollView.addSubview(line4)
        
        
        //提交
        var signBtn=UIButton.init(frame: CGRect(x: 17, y: line4.frame.origin.y+1+55, width: SCREEN_WIDTH-34, height: 50))
        
        
        signBtn.setTitle("提交", for: .normal)
        signBtn=(gradientColor(colo1: "ff8e4a", colo2: "ff6635", view: signBtn,width: SCREEN_WIDTH-34) as? UIButton)!
        
        signBtn.titleLabel?.font=UIFont.systemFont(ofSize: 15)
        signBtn.setTitleColor(UIColor.init(hexString: "ffffff"), for: .normal)
        signBtn.layer.cornerRadius=5
        signBtn.layer.masksToBounds=true
        signBtn.addTarget(self, action: #selector(post), for: .touchUpInside)
        scrollView.addSubview(signBtn)
        
        
    }

   
    
    @objc func post(){
        if PhoneText.text == ""{
            YProgressHUD.showToast("请正确输入手机号")
        }
        else if MessageText.text == ""{
            YProgressHUD.showToast("请输入验证码")
        }
        else if pwdText.text == ""{
            YProgressHUD.showToast("请输入密码")
        }
        else if RePwdText.text == ""{
            YProgressHUD.showToast("请再次输入密码")
        }
        else if RePwdText.text != pwdText.text{
            YProgressHUD.showToast("两次输入密码不相同")
        }
       
        else{
            if type == "1"{
                let dic=["phone":PhoneText.text,"code":MessageText.text,"pwd":pwdText.text,"pwd1":RePwdText.text]
//                Network.request(.jjrForgetPwd(dic)) { (JSON) in
//                    YProgressHUD.showToast(JSON["msg"].string)
//                    self.navigationController?.popViewController(animated: true)
//                } failure: { (er) in
//
//                }

            }
            
            if type == "2"{
                let dic=["phone":PhoneText.text,"code":MessageText.text,"pwd":pwdText.text,"pwd1":RePwdText.text]
//                Network.request(.fwsForgetPwd(dic)) { (JSON) in
//                    YProgressHUD.showToast(JSON["msg"].string)
//                    self.navigationController?.popViewController(animated: true)
//                } failure: { (er) in
//
//                }
            }
        }
    }
    

    @objc func getVerify() {
        
        if PhoneText.text == "" {
            YProgressHUD.showToast("请输入手机号码")
        }else{
            if type == "1"{
                codeType="5"
            }
            if type == "2"{
                codeType="6"
            }
            let dic=["phone":PhoneText.text,"type":codeType]
//            Network.request(.getMessage(dic)) { (JSON) in
//                YProgressHUD.showToast(JSON["msg"].string)
//                if JSON["msg"].string == "发送成功"{
//                    self.MessageBtn.isEnabled=false
//                    self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(1), target: self, selector: #selector(self.tickDown), userInfo: nil, repeats: true)
//                }
//               
//            } failure: { (err) in
//                
//            }
        }
        
    }
    
    
    @objc func tickDown() {
        
        //将剩余时间减少1秒
        
        leftTime -= 1
        
        print(leftTime)
        
        //修改button的剩余时间

        MessageBtn.setTitle(String(leftTime)+"s", for: .normal)
       
        
        //如果剩余时间小于等于0
        
        if leftTime <= 0 {
            
            //取消定时器
            
            timer.invalidate()
            
            MessageBtn.isEnabled = true
            MessageBtn.setTitle("获取验证码", for: .normal)
            leftTime = 60
        }
        
    }

    
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden=true
    }
}
